﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;
using System.Collections.Generic;

namespace PM.Site.Admin
{
    public partial class TradesOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            } 

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                TradeCollection trades = TradeServices.GetTradeCollection();
                dlTrades.DataSource = trades;
                dlTrades.DataBind();

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["tradeDbKey"] = null;
            Response.Redirect("~/Admin/TradesForm.aspx");
        }

        protected void dlTrades_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string tradeDbKey = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "EditTrade":
                    Session["tradeDbKey"] = tradeDbKey;
                    Response.Redirect("~/Admin/TradesForm.aspx");
                    break;
            }

        }
    }
}
